<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">

</head>
<body>
	<div th:replace="gm/common :: html"></div>

	<div id="page-wrapper">
		<div class="graphs">
			<div class="xs">
				<h3>服务器列表管理</h3>
				<hr />
				<br /> 选择平台：<input id="platform" class="easyui-combobox"
					name="language" style="width: 10%;"
					data-options="
                    url:'/gm-center/layout/getPlatform',
                    method:'get',
                    valueField:'id',
                    textField:'text',
                    panelHeight:'auto',
                    labelPosition: 'top',
                    onChange:selectPlatform
                    ">
				<br /> <br />
				<div class="tab-content">
					<div class="tab-pane active" id="horizontal-form">
						<table class="easyui-datagrid" title="服务器列表" id="ServerListPanel"
							style="width: 95%; height: 600px;"
							data-options="singleSelect:true,pagination:true,collapsible:true,url:'/gm-center/game/getServerList',method:'get'">
							<thead>
								<tr>
									<th data-options="field:'serverId',width:80,align:'center'">服务器
										ID</th>
									<th data-options="field:'name',width:170,align:'center'">服务器名称</th>
									<th data-options="field:'ip',width:150,align:'center'">IP地址</th>
									<th data-options="field:'port',width:80,align:'center'">Game端口号</th>
									<th data-options="field:'globalPort',width:80,align:'center'">Http端口号</th>
									<th
										data-options="field:'lastDeployTime',width:150,align:'center'">最近部署时间</th>
									<th
										data-options="field:'lastExcelExportTime',width:150,align:'center'">最近导表时间</th>
									<th data-options="field:'excelVersion',width:80,align:'center'">数值版本</th>
									<th data-options="field:'branchName',width:330,align:'center'">代码分支</th>
									<th
										data-options="field:'_operate',width:350,align:'center',formatter:formatOper">操作</th>
								</tr>
							</thead>
						</table>
					</div>

					<div id="dlg" class="easyui-dialog" title="上传Lua配置"
						data-options="iconCls:'icon-save',resizable:true,modal:true,closed:true"
						style="width: 600px; height: 300px; padding: 20px">

						<div align="center" style="margin-top: 30px;">

							<form id="uploadForm" action="/upload"
								enctype="multipart/form-data">
								<h4 style="color: red">只能上传zip文件，且压缩文件里面包括config文件夹</h4>
								<input type="file" id="file" name="file"
									style="width: 200px; height: 80px;"><br>
							</form>

							<button id="SubmitUploadBattleConfig" type="button"
								class="easyui-linkbutton" value="上传">上传</button>
						</div>
					</div>
					<div id="dlgPro" class="easyui-dialog" title="正在处理..."
						data-options="iconCls:'icon-save',resizable:true,modal:true,closed:true"
						style="width: 600px; height: 300px; padding: 20px">

						<div align="center" style="margin-top: 30px;" id="dlgDiv"></div>
					</div>

					<div id="deployJavaDlg" class="easyui-dialog" title="部署GameServer"
						data-options="iconCls:'icon-save',resizable:true,modal:true,closed:true"
						style="width: 800px; height: 300px; padding: 20px">
						选择或输入代码版本:<input class="easyui-combobox" id="serverVersion"
							name="serverVersion" style="width: 350px;"
							data-options="valueField: 'label',textField: 'value',data: [{
									label: 'dev',
									value: 'dev'
									},{
									 label: 'master',
									value: 'master'
									}]" />
						<button id="SubmitDeployGameServer" class="easyui-linkbutton"
							type="button" value="部署并重启" onclick="submitDeployGameServer()">部署并重启</button>
					</div>

					<div id="clientLuaDlg" class="easyui-dialog"
						title="选择Client Lua Tag"
						data-options="iconCls:'icon-save',resizable:true,modal:true,closed:true"
						style="width: 600px; height: 500px; padding: 20px">

						<div align="center" style="margin-top: 30px;">
							下面两种方式二选其一更新lua

							<div style="margin-top: 20px;">
								<hr />
								<form id="uploadBattleLuaForm" action="/upload"
									enctype="multipart/form-data">
									<h4 style="color: red">只能上传zip文件，且压缩文件里面不包括外层文件夹</h4>
									<table>
										<tr>
											<td><input type="file" id="battleLuafile"
												name="battleLuafile"></td>
											<td>
												<button id="SubmitUploadBattleLua" class="easyui-linkbutton"
													type="button" value="上传">上传Lua代码</button>
											</td>
										</tr>
									</table>
								</form>
							</div>

							<div style="margin-top: 30px;">
								<hr />
								<h5>只显示每个tag的前5个版本，如果没有，请手动输入</h5>
								选择tag:<input class="easyui-combobox" id="clientLuaTagCombox"
									name="clientLuaTagCombox"
									data-options="valueField:'id',textField:'text'"
									style="width: 300px" />
								<button id="SubmitDeployBattleCode" type="button"
									class="easyui-linkbutton" value="开始部署">开始部署Lua代码</button>

							</div>

						</div>
					</div>

				</div>
			</div>
			<!-- /#page-wrapper -->
		</div>
	</div>
	<div id="UpdateGameServerConfigDlg" class="easyui-dialog" title="更新游戏服配置表"
		data-options="resizable:true,modal:true,closed:true"
		style="width: 700px; height: 300px; padding: 10px">
						<h3>Excel数值导表</h3>
				<hr/>
				<h5 style="color:red;">如果添加了新的数值分支名，请通知军少(张军)，王广帅添加相应的操作！！！</h5>
				<hr/>
				选择Excel文件夹: <label for="excelDir"></label> <select id="excelDir">
					<option value="Development">Development</option>
					<option value = "Release">Release</option> 
					<option value = "master">master</option> 
					<option value = "test">test</option>
					<option value = "VersionApply_New">VersionApply_New</option>
					<option value = "majia">majia</option>
				</select> 
					
				<button id="exportStart" type="button" class="easyui-linkbutton"
					value="更新数值">更新数值</button>
	</div>

	<div id="ActionSelectDlg" class="easyui-dialog" title="功能选择"
		data-options="resizable:true,modal:true,closed:true"
		style="width: 700px; height: 300px; padding: 10px">
		<div align="center">
		<table>
			<tr height="60" align="left">
				<th><a href="javascript:void(0)" class="easyui-linkbutton"
					id="DeployAndRestartBtn">部署并重启此服务</a> <a href="javascript:void(0)"
					class="easyui-linkbutton" id="RestartServerBtn">重启此服务</a> <a
					href="javascript:void(0)" class="easyui-linkbutton"
					id="StartServerBtn">启动此服务</a> <a href="javascript:void(0)"
					class="easyui-linkbutton" id="ShutdownServerBtn">关闭此服务</a></th>
			</tr>
			<tr height="60" align="left">
				<th>
				<a href="javascript:void(0)" class="easyui-linkbutton"
					id="UpdateGameServerConfigBtn">更新游戏服数值配置</a>
				<a href="javascript:void(0)" class="easyui-linkbutton"
					id="UploadLuaConfigBtn">上传Lua战斗配置</a> 
					<a href="javascript:void(0)"
					class="easyui-linkbutton" id="UploadLuaBattleBtn">更新Lua战斗代码</a>
					
			    </th>
			</tr>
			<tr height="60" align="left">
				<th><a href="javascript:void(0)" class="easyui-linkbutton"
					id="ClearRankBtn">清空排行榜</a> 
					<a href="javascript:void(0)"
					class="easyui-linkbutton" id="ClearChatBtn">清空聊天记录</a>
					
					<a href="javascript:void(0)"
					class="easyui-linkbutton" id="ClearArenaRobotBtn">清空竞技场机器人</a>
				</th>
			</tr>
			<tr height="60" align="left">
			   <th>
			   <a href="javascript:void(0)"
					class="easyui-linkbutton" id="ClearAllDataBtn">清空清档</a>
			   </th>
			</tr>
		</table>
	</div>
	</div>
	
	<script type="text/javascript">
		function formatOper(val, row, index) {
			var my = '<input type="button" onClick="actionSelect('
					+ index
					+ ')" style="font-size:40px;width:150px;height:55px;" value="--选择操作--"/>';
			return my;
		}
		function actionSelect(index) {
			$("#ServerListPanel").datagrid("selectRow", index);
			$("#ActionSelectDlg").dialog("open");
			var row = $("#ServerListPanel").datagrid("getSelected");

		}
		function clearRedis(type) {
			$.messager.confirm('警告', '你确认要执行这个操作吗？数据不可恢复哦！！', function(r) {
				if (r) {
					var url = "/node/" + selectServerId + "/game/clearRedis/"
					+ selectServerId + "/" + type;
					var json = {};
					postAjax(url,json,function(data){
						$.messager.alert('提示', data);
					});
				}
			});

		}

		function resetServerTime(index) {
			setSelectRow(index);
			$.messager.confirm('确认', '你确定要重置服务器时间吗?', function(r) {
				if (r) {
					$.ajax({
						type : 'post',
						url : "/node/" + selectServerId
								+ "/game/resetServerTime/" + selectServerId,

						cache : false,

						success : function(data) {
							alert("重置成功：" + data.result);
						},
						error : function(data) {
							alert("重置失败,联系服务端程序！！");
						}
					});
				}
			});

		}
		function submitDeployGameServer() {
			var row = $("#ServerListPanel").datagrid("getSelected");
			$.messager.confirm('警告', '你确认部署并重启['+ row.name +']吗？', function(r) {
				if (r) {
					var serverVersion = $("#serverVersion").combobox("getValue");
						var serverId = row.serverId;
						var platform = $("#platform").val();
						var url = "/gm-center/game/deploy/" + serverId;
						var json = {"branchName" : serverVersion};
						postAjax(url,json,function(data){
							$("#deployJavaDlg").dialog("close");
							$.messager.alert('提示', '正在部署服务器，请关注盯盯通知！！');
						});
				}
			});
		}
function serverOpreation(type){
	var row = $("#ServerListPanel").datagrid("getSelected");
	$.messager.confirm('确认', '你确定要操作[ '+ row.name +' ]服务吗?', function(r) {
		if (r) {
			alert("正在重启服务器，请关注盯盯通知！！")
			var selectServerId=row.serverId;
			var url = "/node/" + selectServerId + "/game/restartServer/" + selectServerId + "/" + type;
			var json = {};
			postAjax(url,json,function(data){
				$.messager.alert('提示', '正在重启服务器，请关注盯盯通知！！');
			});
		}
	});
}
$(document).ready(function() {
	$("#DeployAndRestartBtn").click(function(){
		$("#deployJavaDlg").dialog("open");
	});
	$("#RestartServerBtn").click(function(){
		serverOpreation(0);
	});
	$("#StartServerBtn").click(function(){
		serverOpreation(1);
	});
	$("#ShutdownServerBtn").click(function(){
		serverOpreation(2);
	});
	$("#UploadLuaConfigBtn").click(function(){
		$("#dlg").dialog("open");
	});
	$("#UploadLuaBattleBtn").click(function(){
		var row = $("#ServerListPanel").datagrid("getSelected");
		var selectServerId = row.serverId;
		var url = "/node/" + selectServerId + "/game/getClientLuaAllTags";
		$("#clientLuaTagCombox").combobox("reload", url);
		$("#clientLuaDlg").dialog("open");
	});
	$("#ClearRankBtn").click(function(){
		clearRedis(2);
	});
	$("#ClearChatBtn").click(function(){
		clearRedis(2);
	});
	$("#ClearArenaRobotBtn").click(function(){
		clearRedis(1);
	});
	$("#ClearAllDataBtn").click(function(){
		clearRedis(3);
	});
	$("#UpdateGameServerConfigBtn").click(function(){
		$("#UpdateGameServerConfigDlg").dialog("open");
	});

	$("#file").change(function() {
		var filepath = $("input[name='file']").val();
		var extStart = filepath.lastIndexOf(".");
		var ext = filepath.substring(extStart,filepath.length).toUpperCase();
		if (ext != ".ZIP") {
			alert("对不起，只能上传zip的文件，去压缩一下吧！！");
			return false;
		}
	});
	
	 $("#exportStart").click(function(){
		 var row = $("#ServerListPanel").datagrid("getSelected");
		 var serverId = row.serverId;
		 
		 var excelDir = $("#excelDir").val();
		 $.ajax({
             type: "POST",
             url: "/gm-center/game/exportData/" + serverId + "/" + excelDir,
             dataType: "json",
             contentType : "application/json",
             success: function(data){
            	 if(data.result == "ok"){
            		 $("#UpdateGameServerConfigDlg").dialog("close");
            		 alert("已开始导出，请关注盯盯群通知");
            	 } else {
            		 alert("服务器出错：" + JSON.stringify(data));
            	 }
             },
             error: function(data){
            	 alert("请求失败：" + JSON.stringify(data));
             }
         });
	 });
	
							$("#SubmitUploadBattleConfig")
									.click(
											function() {
												var row = $("#ServerListPanel").datagrid("getSelected");
												var selectServerId = row.serverId;
												$("#dlg").dialog("close");
												$("#dlgPro").dialog("open");
												$("#dlgDiv").html(
														"正在上传中，请稍等...");
												var formData = new FormData(
														$('#uploadForm')[0]);
												$
														.ajax({
															type : 'post',
															url : "/node/"
																	+ selectServerId
																	+ "/game/uploadBattleConfig/"
																	+ selectServerId,
															data : formData,
															cache : false,
															processData : false,
															contentType : false,
															success : function(
																	data) {
																if (data.result == "ok") {
																	$("#dlgDiv")
																			.html(
																					"上传成功，重启服务器生效。去重启吧!!");
																} else {
																	$("#dlgPro")
																			.dialog(
																					"close");
																	alert("请求失败："
																			+ JSON
																					.stringify(data));
																}

															},
															error : function(
																	data) {
																$("#dlgPro")
																		.dialog(
																				"close");
																alert("请求失败："
																		+ JSON
																				.stringify(data));
															}
														});
											});
							$("#SubmitUploadBattleLua")
									.click(
											function() {
												var row = $("#ServerListPanel").datagrid("getSelected");
												var selectServerId = row.serverId;
												$("#clientLuaDlg").dialog(
														"close");
												$("#dlgPro").dialog("open");
												$("#dlgDiv").html(
														"正在上传中，请稍等...");
												var formData = new FormData(
														$('#uploadBattleLuaForm')[0]);
												$
														.ajax({
															type : 'post',
															url : "/node/"
																	+ selectServerId
																	+ "/game/uploadBattleLua/"
																	+ selectServerId,
															data : formData,
															cache : false,
															processData : false,
															contentType : false,
															success : function(
																	data) {
																if (data.result == "ok") {
																	$("#dlgDiv")
																			.html(
																					"上传成功，重启服务器生效。去重启吧!!");
																} else {
																	$("#dlgPro")
																			.dialog(
																					"close");
																	alert("请求失败："
																			+ JSON
																					.stringify(data));
																}

															},
															error : function(
																	data) {
																$("#dlgPro")
																		.dialog(
																				"close");
																alert("请求失败："
																		+ JSON
																				.stringify(data));
															}
														});
											});

							$("#SubmitDeployBattleCode")
									.click(
											function() {
												var row = $("#ServerListPanel").datagrid("getSelected");
												var selectServerId = row.serverId;
												$("#clientLuaDlg").dialog(
														"close");
												$("#dlgPro").dialog("open");
												$("#dlgDiv")
														.html(
																"正在部署中，请稍等,可关注盯盯查看进度...");
												var gitTag = $(
														"#clientLuaTagCombox")
														.combobox("getValue");
												$
														.ajax({
															type : "POST",
															url : "/node/"
																	+ selectServerId
																	+ "/game/exportBattleLua/"
																	+ selectServerId
																	+ "/"
																	+ gitTag,
															dataType : "json",
															contentType : "application/json",
															success : function(
																	data) {
																if (data.result == "ok") {
																	$("#dlgDiv")
																			.html(
																					"部署成功，重启服务器生效!!");
																} else {
																	$("#dlgDiv")
																			.html(
																					"部署失败,"
																							+ data.result);
																}
															},
															error : function(
																	data) {
																$("#dlgDiv")
																		.html(
																				"部署失败,"
																						+ JSON
																								.stringify(data));
															}
														});
											});
						});

		function selectPlatform() {
			$("#ServerListPanel").datagrid('load', {
				platform : $("#platform").val()
			});
		}
	</script>
</body>
</html>